package com.ztz.mynewdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.activity.SocietyActivity;
import com.ztz.mynewdemo.bean.SocietyResponse;

import java.util.List;

/**
 * Created by wqewqe on 2017/5/18.
 */

public class SocietyAdapter extends RecyclerView.Adapter<SocietyAdapter.ViewHolder> {
    Context context;
    List<SocietyResponse.ResultBean.DataBean> dataBeanList;

    public SocietyAdapter(Context context, List<SocietyResponse.ResultBean.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_topline,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                Intent intent=new Intent(context, SocietyActivity.class);
                intent.putExtra(SocietyActivity.SOCIETY_ID,dataBeanList.get(position).getUrl());
                intent.putExtra(SocietyActivity.TITLE,dataBeanList.get(position).getTitle());
                intent.putExtra(SocietyActivity.DATE,dataBeanList.get(position).getDate());
                intent.putExtra(SocietyActivity.CATEGORY,dataBeanList.get(position).getCategory());
                intent.putExtra(SocietyActivity.AUTHOR,dataBeanList.get(position).getAuthor_name());
                intent.putExtra(SocietyActivity.IMAGE,dataBeanList.get(position).getThumbnail_pic_s());
                context.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(context).load(dataBeanList.get(position).getThumbnail_pic_s()).into(holder.ivTop);
        holder.top_title.setText(dataBeanList.get(position).getTitle());
        holder.top_data.setText(dataBeanList.get(position).getDate());
        holder.top_category.setText(dataBeanList.get(position).getCategory());
        holder.top_author.setText(dataBeanList.get(position).getAuthor_name());
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout item;

        ImageView ivTop;
        TextView top_title;
        TextView top_data;
        TextView top_category;
        TextView top_author;

        public ViewHolder(View view) {
            super(view);
            item=(LinearLayout)view;

            ivTop=(ImageView)view.findViewById(R.id.iv_top);
            top_title=(TextView)view.findViewById(R.id.top_title);
            top_data=(TextView)view.findViewById(R.id.topLine_data);
            top_category=(TextView)view.findViewById(R.id.tv_category);
            top_author=(TextView)view.findViewById(R.id.tv_author);
        }
    }
}
